﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public partial class Menu
    {
        private bool _ViewApprovedItem = false;
        public bool ViewApprovedItem
        {
            get { return _ViewApprovedItem; }
            set { _ViewApprovedItem = value; }
        }
    }

    public partial class Menu:IEquatable<User>
    {
        public const string WEB_TYPE = "Web";
        public const string WINFORM_TYPE = "WinForm";

        private List<Menu> _ListSubMenu = null;
        public List<Menu> ListSubMenu
        {
            get
            {
                if(_ListSubMenu == null)
                    _ListSubMenu = new List<Menu>();
                return _ListSubMenu;
            }
            set
            {
                _ListSubMenu = value;
            }
        }
        
        public bool Equals(User other)
        {
            if (Object.ReferenceEquals(other, null)) return false;
            if (Object.ReferenceEquals(this, other)) return true;
            return ID.Equals(other.ID);
        }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        public Common.ERROR_CODE.ERROR_CODE_SIMPLE GetSubMenu(ref string MessageSystemError, ref List<Menu> ListRootMenu, List<Menu> ListUserMenu)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                Queue<Menu> queueMenu = new Queue<Menu>();
                foreach (Menu item in ListRootMenu)
                {
                    queueMenu.Enqueue(item);
                }

                while (queueMenu.Count > 0)
                {
                    Menu currMenu = queueMenu.Dequeue();
                    List<Menu> listSubMenu = new List<Menu>();
                    foreach (Menu temp in ListUserMenu)
                    {
                        if (temp.ParentID == currMenu.ID)
                        {
                            listSubMenu.Add(temp);
                            queueMenu.Enqueue(temp);
                        }
                    }
                    currMenu.ListSubMenu = listSubMenu;
                }
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SUCCESS;
            }
            catch(Exception ex)
            {
                MessageSystemError = Common.ExceptionUtils.ExceptionToMessage(ex);
                return Common.ERROR_CODE.ERROR_CODE_SIMPLE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }
    }
}
